package cn.dankal.flyaudio.dialog;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.DialogFragment;

import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import cn.dankal.basiclib.DankalApplication;
import cn.dankal.basiclib.common.share.mob.ShareDialog2;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.Util;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.friend.ChatShareActivity;
import cn.dankal.flyaudio.friend.utils.ShareUtils;

public class PersonalSignSuccessDialog extends DialogFragment implements View.OnClickListener {

    private OnCloseListener listener;
    private IWXAPI api;

    private ConstraintLayout clTitle;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = DankalApplication.instance().getIWXAPI();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_sign_success, container, false);
        view.findViewById(R.id.iv_close).setOnClickListener(this);
        view.findViewById(R.id.ll_friend).setOnClickListener(this);
        view.findViewById(R.id.ll_wx_friend).setOnClickListener(this);
        view.findViewById(R.id.ll_wx_friend_circle).setOnClickListener(this);
        clTitle = view.findViewById(R.id.cl_title);

        return view;
    }


    public void setOnCloseListener(OnCloseListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.iv_close:
                dismiss();
                if (listener != null) {
                    listener.onClose();
                }
                break;
            case R.id.ll_friend:
                ShareUtils.getInstance().setShareBitmap(convertViewToBitmap(clTitle));
                ActivityUtils.startActivity(ChatShareActivity.class);
                break;
            case R.id.ll_wx_friend:
                ivWeChat(1,convertViewToBitmap(clTitle));
                break;
            case R.id.ll_wx_friend_circle:
                ivWeChat(2,convertViewToBitmap(clTitle));
                break;
        }
    }

    public Bitmap convertViewToBitmap(View view) {

        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),

                Bitmap.Config.ARGB_8888);

//利用bitmap生成画布

        Canvas canvas = new Canvas(bitmap);

//把view中的内容绘制在画布上

        view.draw(canvas);

        return bitmap;

    }

    //分享到朋友圈或者朋友圈
    private void ivWeChat(int type) {
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = "www.baidu.com";
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = "此刻 ";
        msg.description = "百度一下";
        Bitmap thumbBmp = BitmapFactory.decodeResource(getContext().getResources(), cn.dankal.basiclib.R.mipmap.qq_refresh_success);
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        if (type == 1) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
        api.sendReq(req);
    }
    //分享到朋友圈或者朋友圈
    private void ivWeChat(int type, Bitmap bitmap) {
        api = DankalApplication.instance().getIWXAPI();
        WXImageObject imgObj = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

//设置缩略图
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
        bitmap.recycle();
        msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

//构造一个Req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        if (type == 1) {
            req.scene = SendMessageToWX.Req.WXSceneSession;
        } else {
            req.scene = SendMessageToWX.Req.WXSceneTimeline;
        }
//调用api接口，发送数据到微信
        api.sendReq(req);
    }
    public interface OnCloseListener {
        void onClose();
    }
}
